package com.yxzr.youyou.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.yxzr.youyou.R;
import com.yxzr.youyou.bean.CommentBean;
import com.yxzr.youyou.retrofit.DataUrl;
import com.yxzr.youyou.utils.DateTimeUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolder> {
    private Context mContext;
    private List<CommentBean.DataBean> list = new ArrayList<>();
    public CommentAdapter(Context context){
        mContext = context;
    }

    public void addList(List<CommentBean.DataBean> mlist){
        if (mlist != null && mlist.size() > 0){
            list.addAll(mlist);
            notifyDataSetChanged();
        }
    }

    public void clearList(){
        list.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.comment_adapter_layout,parent,false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Glide.with(mContext)
                .setDefaultRequestOptions(
                        new RequestOptions()
                                .frame(1000000)
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .centerCrop()
                                .error(R.mipmap.default_avatar)
                                .placeholder(R.mipmap.default_avatar))
                .load(DataUrl.imgUrl+list.get(position).getHeadPic())
                .apply(RequestOptions.bitmapTransform(new CropCircleTransformation()))
                .into(holder.comment_img);

        holder.comment_tv_name.setText(list.get(position).getUsername());
        if (!TextUtils.isEmpty(list.get(position).getCreateTime())){
            holder.comment_tv_time.setText(DateTimeUtil.formatFriendly(new Date(Long.parseLong(list.get(position).getCreateTime()) * 1000)));
        }

        holder.comment_tv_content.setText(list.get(position).getContent());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView comment_img;
        private TextView comment_tv_name,comment_tv_time,comment_tv_content;
        public ViewHolder(View itemView) {
            super(itemView);
            comment_img = itemView.findViewById(R.id.comment_img);
            comment_tv_name = itemView.findViewById(R.id.comment_tv_name);
            comment_tv_time = itemView.findViewById(R.id.comment_tv_time);
            comment_tv_content = itemView.findViewById(R.id.comment_tv_content);
        }
    }
}
